package com.jj.Dialect;

import com.jj.entity.menu;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.engine.AttributeName;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.standard.processor.AbstractStandardConditionalVisibilityTagProcessor;
import org.thymeleaf.templatemode.TemplateMode;

import java.util.List;

public class RoleBtnShowProcessor extends AbstractStandardConditionalVisibilityTagProcessor {
    private RedisTemplate redisTemplate;
    protected RoleBtnShowProcessor(RedisTemplate redisTemplate) {
        super(TemplateMode.HTML, "bs", "btn", 1000);
        this.redisTemplate=redisTemplate;
    }

    @Override
    protected boolean isVisible(ITemplateContext context, IProcessableElementTag elementTag, AttributeName attributeName, String s) {
        //获取页面中按钮的权限Code
        String value = elementTag.getAttributeValue(attributeName);
        System.out.println(value);
        //从redis中获取用户权限的code
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        List<menu> menus = (List<menu>) redisTemplate.opsForValue().get("se");
        //遍历比较
        for (menu menu : menus) {
            if (menu.getMenu_code().equals(value)){
                return true;
            }
        }
        return false;
    }
}